<?xml version="1.0" encoding="UTF-8"?>
<ui:composition template="/pages/mastertemplate/MasterPage.xhtml"
	xmlns="http://www.w3.org/1999/xhtml"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:p="http://primefaces.org/ui" xmlns:syn="http://www.syniverse.com">
	<ui:define name="content">
		<p:panel header="#{msg.donHangTitle}">

			<h:form>
				<div>
					<p:selectOneRadio id="options"
						value="#{DonHangBean.loaiFile}">
						<f:selectItem for="options"
							itemLabel="#{msg.donHang}" itemValue="DonHang" />
						<f:selectItem for="options"
							itemLabel="#{msg.chiTietDonHang}" itemValue="ChiTietDonHang" />
					</p:selectOneRadio>
				</div>
			</h:form>
			<h:form>
				<p:fileUpload widgetVar="fileuplaod_wgt"
					fileUploadListener="#{DonHangBean.handleFileUpload}"
					mode="advanced" dragDropSupport="false" update=":donHangForm"
					sizeLimit="100000" fileLimit="1"
					allowTypes="/(\.|\/)(#{DataConfigBean.fileExtensionPattern})$/" />
			</h:form>

			<h:form>
				<p:commandButton style="width:150px;float: left;margin-top:10px;"
					value="#{msg.Button_export}"
					actionListener="#{DonHangBean.downloadFileCSV}" ajax="false">
					<p:fileDownload value="#{DonHangBean.file}" />
				</p:commandButton>
			</h:form>

			<h:form id="donHangForm">
				<p:growl id="growforFile" autoupdate="true"
					life="#{DataConfigBean.growlShow}" />

				<p:dataTable id="dataTable" var="dHang"
					value="#{DonHangBean.listDonHang}" paginator="true" rows="10"
					paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
					rowsPerPageTemplate="10,20,50,100"
					filteredValue="#{DonHangBean.listDonHangFiltered}"
					style="width: 100%; margin: 45px auto;" widgetVar="DonHangTable">

					<f:facet name="header">
						<h:outputText value="#{msg.danhSachDonHang}"
							styleClass="tableHeader" />
						<div style="margin: -4px -11px 5px; float: right;">
							<p:commandButton style="height: 25px"
								update=":addDonHangForm:display" value="+ thêm mới"
								rendered="#{UserBean.getAccessLevel() == 'WRITE'}"
								actionListener="#{DonHangBean.notifyAdd}"
								onclick="PF('donHangDialog').show();">
							</p:commandButton>
						</div>

					</f:facet>

					<p:column headerText="#{msg.soChungTu}"
						filterBy="#{dHang.soChungTu}" filterMatchMode="contains">#{dHang.soChungTu}  </p:column>

					<p:column headerText="#{msg.loaiDonHang}"
						filterBy="#{dHang.loaiDonHang}" filterMatchMode="exact"
						id="loaidonHang">
						<h:outputText value="#{dHang.loaiDonHang}" />
					</p:column>

					<p:column headerText="#{msg.ngayLap}" filterBy="#{dHang.ngayLap}"
						filterMatchMode="contains">#{dHang.ngayLap}  </p:column>


					<p:column headerText="#{msg.phuongTienVanChuyen}"
						filterBy="#{dHang.phuongTienVanChuyen}" filterMatchMode="contains">#{dHang.phuongTienVanChuyen}  </p:column>

					<p:column headerText="#{msg.nguoiGiaoHang}"
						filterBy="#{dHang.nguoiGiaoHang}" filterMatchMode="contains">#{dHang.nguoiGiaoHang}  </p:column>

					<p:column headerText="#{msg.soDTNguoiGiaoHang}"
						filterBy="#{dHang.soDTNguoiGiaoHang}" filterMatchMode="contains">#{dHang.soDTNguoiGiaoHang}  </p:column>

					<p:column headerText="#{msg.congTy}" filterBy="#{dHang.doiTac}"
						filterMatchMode="contains">#{dHang.doiTac}  </p:column>

					<p:column headerText="#{msg.caNhan}" filterBy="#{dHang.caNhan}"
						filterMatchMode="contains">#{dHang.caNhan}  </p:column>

					<p:column headerText="#{msg.trangthai}"
						filterBy="#{dHang.trangThai}" filterMatchMode="contains">#{dHang.trangThai}  </p:column>



					<p:column headerText="#{msg.table_actions}"
						style="text-align:center;min-width:10px">

						<p:commandButton styleClass="smallButton"
							icon="ui-icon ui-icon-pencil" update=":addDonHangForm:display"
							rendered="#{UserBean.getAccessLevel() == 'WRITE'}"
							actionListener="#{DonHangBean.notifyEdit(dHang)}"
							oncomplete="PF('donHangDialog').show();">
						</p:commandButton>

						<p:commandButton style="margin-left: 5px" styleClass="smallButton"
							icon="ui-icon ui-icon-trash"
							rendered="#{UserBean.getAccessLevel() == 'WRITE'}"
							oncomplete="PF('confirmRemove').show();">
							<f:setPropertyActionListener value="#{dHang}"
								target="#{DonHangBean.selectedDonHang}" />
						</p:commandButton>


					</p:column>
				</p:dataTable>
			</h:form>

			<h:form id="addDonHangForm">
				<p:growl id="message" autoupdate="true"
					life="#{DataConfigBean.growlShow}" />

				<p:dialog id="addDonHangDialog" header="Thêm mới/ Sửa" modal="true"
					widgetVar="donHangDialog" resizable="false">
					<p:scrollPanel id="display" mode="native"
						style="width:800px;height:500px">

						<h:panelGrid columns="2" id="infoForm">
							<h:panelGrid columns="2">
								<p:outputLabel style="margin-left: 20px"
									value="#{msg.loaiDonHang}" />
								<p:selectOneMenu styleClass="inputText"
									value="#{DonHangBean.donHang.loaiDonHang}" filter="true"
									filterMatchMode="contains" required="true">
									<f:selectItem itemLabel="Select One" itemValue=""></f:selectItem>
									<f:selectItems value="#{DonHangBean.listLoaiDonHang}"
										var="selectItem" itemLabel="#{selectItem}"
										itemValue="#{selectItem}" />
									<p:ajax event="change" update=":addDonHangForm:infoForm"
										listener="#{DonHangBean.changeLoaiDonHang}"></p:ajax>
								</p:selectOneMenu>
							</h:panelGrid>
							<h:panelGrid columns="2">
								<p:outputLabel style="margin-left: 20px" for="soChungTu"
									value="#{msg.soChungTu}" />
								<p:inputText styleClass="inputText" id="soChungTu"
									value="#{DonHangBean.donHang.soChungTu}">
								</p:inputText>
							</h:panelGrid>

							<h:panelGrid columns="2">
								<p:outputLabel style="margin-left: 20px" value="#{msg.ngayLap}" />
								<p:calendar style="min-width:140px"
									value="#{DonHangBean.donHang.ngayLap}"
									pattern="#{DataConfigBean.datepattern}" showButtonPanel="true">
									<syn:convertTimestamp pattern="#{DataConfigBean.datepattern}" />
								</p:calendar>
							</h:panelGrid>
							<h:panelGrid columns="2"
								rendered="#{DonHangBean.loaiDonHang=='MUA_HANG' or DonHangBean.loaiDonHang=='BAN_HANG' }">
								<p:outputLabel style="margin-left: 20px"
									value="#{msg.phuongTienVanChuyen}" />
								<p:selectOneMenu styleClass="inputText"
									value="#{DonHangBean.donHang.phuongTienVanChuyen}"
									filter="true" filterMatchMode="contains">
									<f:selectItems value="#{DonHangBean.listPhuongTienVanChuyen}"
										var="selectItem" itemLabel="#{selectItem}"
										itemValue="#{selectItem}" />
								</p:selectOneMenu>
							</h:panelGrid>
							<h:panelGrid columns="2"
								rendered="#{DonHangBean.loaiDonHang=='MUA_HANG' or DonHangBean.loaiDonHang=='BAN_HANG' }">
								<p:outputLabel style="margin-left: 20px"
									value="#{msg.nguoiGiaoHang}" />
								<p:inputText styleClass="inputText"
									value="#{DonHangBean.donHang.nguoiGiaoHang}">
								</p:inputText>
							</h:panelGrid>
							<h:panelGrid columns="2"
								rendered="#{DonHangBean.loaiDonHang=='MUA_HANG' or DonHangBean.loaiDonHang=='BAN_HANG' }">
								<p:outputLabel style="margin-left: 20px"
									value="#{msg.soDTNguoiGiaoHang}" />
								<p:inputText styleClass="inputText"
									value="#{DonHangBean.donHang.soDTNguoiGiaoHang}">
								</p:inputText>
							</h:panelGrid>
							<h:panelGrid columns="2"
								rendered="#{DonHangBean.loaiDonHang=='MUA_HANG' or DonHangBean.loaiDonHang=='BAN_HANG' }">
								<p:outputLabel style="margin-left: 20px" value="#{msg.congTy}" />
								<p:selectOneMenu styleClass="inputText"
									value="#{DonHangBean.donHang.doiTac}" filter="true"
									filterMatchMode="contains">
									<f:selectItem itemLabel="Select One" itemValue=""></f:selectItem>
									<f:selectItems value="#{DoiTacBean.listDoiTac}"
										var="selectItem" itemLabel="#{selectItem.tenDoiTac}"
										itemValue="#{selectItem.maDoiTac}" />
								</p:selectOneMenu>
							</h:panelGrid>
							<h:panelGrid columns="2"
								rendered="#{DonHangBean.loaiDonHang=='BAN_HANG'}">
								<p:outputLabel style="margin-left: 20px" for="caNhan"
									value="#{msg.caNhan}" />
								<p:inputText styleClass="inputText" id="caNhan"
									value="#{DonHangBean.donHang.caNhan}">
								</p:inputText>
							</h:panelGrid>
							<h:panelGrid columns="2"
								rendered="#{DonHangBean.loaiDonHang=='DUNG_NOI_BO'}">
								<p:outputLabel style="margin-left: 20px"
									value="#{msg.nhanVienNhan}" />
								<p:selectOneMenu styleClass="inputText"
									value="#{DonHangBean.donHang.nhanVienNhan}" filter="true"
									filterMatchMode="contains">
									<f:selectItem itemLabel="" itemValue="" />
									<f:selectItems value="#{NhanVienBean.listNhanVien}"
										var="selectItem" itemLabel="#{selectItem.hoTen}"
										itemValue="#{selectItem.maNhanVien}" />
								</p:selectOneMenu>
							</h:panelGrid>
							<h:panelGrid columns="2"
								rendered="#{DonHangBean.loaiDonHang=='CHUYEN_KHO'}">


								<p:outputLabel style="margin-left: 20px"
									value="#{msg.khoNhanHang}" />
								<p:selectOneMenu styleClass="inputText"
									value="#{DonHangBean.donHang.khoNhanHang}" filter="true"
									filterMatchMode="contains">
									<f:selectItem itemLabel="" itemValue="" />
									<f:selectItems value="#{KhoHangBean.listKhoHang}"
										var="selectItem" itemLabel="#{selectItem.tenKhoHang}"
										itemValue="#{selectItem.maKhoHang}" />
								</p:selectOneMenu>
							</h:panelGrid>
							<h:panelGrid columns="2"
								rendered="#{DonHangBean.loaiDonHang=='CHUYEN_KHO'}">
								<p:outputLabel style="margin-left: 20px"
									value="#{msg.khoXuatHang}" />
								<p:selectOneMenu styleClass="inputText"
									value="#{DonHangBean.donHang.khoXuatHang}" filter="true"
									filterMatchMode="contains">
									<f:selectItem itemLabel="" itemValue="" />
									<f:selectItems value="#{KhoHangBean.listKhoHang}"
										var="selectItem" itemLabel="#{selectItem.tenKhoHang}"
										itemValue="#{selectItem.maKhoHang}" />
								</p:selectOneMenu>
							</h:panelGrid>

							<h:panelGrid columns="2">
								<p:outputLabel style="margin-left: 20px"
									value="#{msg.trangThai}" />

								<p:selectOneMenu styleClass="inputText"
									value="#{DonHangBean.donHang.trangThai}" filter="true"
									filterMatchMode="contains">
									<f:selectItems value="#{DonHangBean.listTrangThai}"
										var="selectItem" itemLabel="#{selectItem}"
										itemValue="#{selectItem}" />
								</p:selectOneMenu>
							</h:panelGrid>
						</h:panelGrid>

						<p:dataTable id="dataTableChiTietDonHang" var="chiTietDonHang"
							value="#{DonHangBean.listPODetail}" lazy="true" paginator="true"
							rows="10"
							paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
							rowsPerPageTemplate="10,20,50,100" style="width: 100%;"
							editable="true">

							<f:facet name="header">
								<h:outputText value="#{msg.danhSachChiTietDonHang}"
									styleClass="tableHeader" />
								<div style="margin: -4px -11px 5px; float: right;">
									<p:commandButton style="height: 25px" value="+ thêm mới"
										rendered="#{UserBean.getAccessLevel() == 'WRITE'}"
										update=":addChiTietDonHangForm:displayChiTietDonHang"
										actionListener="#{DonHangBean.notifyAddChiTietDonHang}"
										onclick="PF('chiTietDonHangDialog').show();">
									</p:commandButton>
								</div>

							</f:facet>

							<p:column headerText="#{msg.soChungTu}">#{chiTietDonHang.donHang} 
											</p:column>

							<p:column headerText="#{msg.maVatTu}">
								<p:cellEditor>
									<f:facet name="output">
										<h:outputText value="#{chiTietDonHang.vatTuHangHoa}" />
									</f:facet>
									<f:facet name="input">
										<p:inputText value="#{chiTietDonHang.vatTuHangHoa}" />
									</f:facet>
								</p:cellEditor>
							</p:column>

							<p:column headerText="#{msg.tenVatTu}">
								<p:cellEditor>
									<f:facet name="output">
										<h:outputText value="#{chiTietDonHang.tenVTHH}" />
									</f:facet>
									<f:facet name="input">
										<p:inputText value="#{chiTietDonHang.tenVTHH}" />
									</f:facet>
								</p:cellEditor>
							</p:column>


							<p:column headerText="#{msg.soLuong}">
								<p:cellEditor>
									<f:facet name="output">
										<h:outputText value="#{chiTietDonHang.soLuong}" />
									</f:facet>
									<f:facet name="input">
										<p:inputText value="#{chiTietDonHang.soLuong}" />
									</f:facet>
								</p:cellEditor>
							</p:column>

							<p:column headerText="#{msg.donGia}">
								<p:cellEditor>
									<f:facet name="output">
										<h:outputText value="#{chiTietDonHang.donGia}" />
									</f:facet>
									<f:facet name="input">
										<p:inputText value="#{chiTietDonHang.donGia}" />
									</f:facet>
								</p:cellEditor>
							</p:column>

							<p:column>

								<p:commandButton style="margin-left: 5px"
									styleClass="smallButton" icon="ui-icon ui-icon-trash"
									rendered="#{UserBean.getAccessLevel() == 'WRITE'}"
									oncomplete="PF('confirmRemoveChiTietDonHang').show();">
									<f:setPropertyActionListener value="#{chiTietDonHang}"
										target="#{DonHangBean.selectedProductOrderDetail}" />
								</p:commandButton>

							</p:column>
						</p:dataTable>

						<p:commandButton value="Lưu" actionListener="#{DonHangBean.save}"
							update=":donHangForm :addDonHangForm:display" process=""
							oncomplete="if(args.opSuccess) {PF('donHangDialog').hide(); PF('DonHangTable').filter()}">

						</p:commandButton>

					</p:scrollPanel>
				</p:dialog>
			</h:form>
			<h:form>
				<p:dialog id="confirmRemove" header="Xác nhận" modal="true"
					widgetVar="confirmRemove">

					<h:outputText style="margin-left: 20px" value="Bạn có chắc không ?" />
					<p:commandButton value="Đồng ý"
						actionListener="#{DonHangBean.removeDonHang}"
						oncomplete="PF('confirmRemove').hide(); PF('DonHangTable').filter()" update=":donHangForm"></p:commandButton>
					<p:commandButton value="Hủy"></p:commandButton>

				</p:dialog>
			</h:form>

			<h:form>
				<p:dialog id="removeCtdhDialog" header="Xác nhận" modal="true"
					widgetVar="confirmRemoveChiTietDonHang">

					<h:outputText style="margin-left: 20px" value="Bạn có chắc không ?" />
					<p:commandButton value="Đồng ý"
						actionListener="#{DonHangBean.removeChiTietDonHang}"
						oncomplete="PF('confirmRemoveChiTietDonHang').hide()"
						update=":addDonHangForm:display"></p:commandButton>
					<p:commandButton value="Hủy"></p:commandButton>

				</p:dialog>
			</h:form>

			<h:form id="addChiTietDonHangForm">
				<p:dialog id="dialogChiTietDonHang" header="Thêm mới/ Sửa"
					modal="true" widgetVar="chiTietDonHangDialog" resizable="false">

					<p:panel id="displayChiTietDonHang">
						<table>
							<tr>
								<td><p:outputLabel style="margin-left: 20px" for="maVatTu"
										value="#{msg.maVatTu}" /></td>
								<td><p:selectOneMenu styleClass="inputText" required="true"
										requiredMessage="#{msg.maVatTu}: #{msg.valuerequired}"
										id="maVatTu" value="#{DonHangBean.maVthh}" filter="true"
										filterMatchMode="contains">
										<f:selectItem itemLabel="Select One" itemValue="">
										</f:selectItem>
										<f:selectItems var="selectItem"
											value="#{VatTuHangHoaBean.listVthh}"
											itemLabel="#{selectItem.tenVTHH}"
											itemValue="#{selectItem.maVTHH}" />
									</p:selectOneMenu></td>
							</tr>

							<tr>
								<td><p:outputLabel style="margin-left: 20px" for="soLuong"
										value="#{msg.soLuong}" /></td>
								<td><p:inputText styleClass="inputText" required="false"
										requiredMessage="#{msg.soLuong}: #{msg.valuerequired}"
										id="soLuong" value="#{DonHangBean.soluong}">
									</p:inputText></td>
							</tr>

							<tr>
								<td><p:outputLabel style="margin-left: 20px" for="donGia"
										value="#{msg.donGia}" /></td>
								<td><p:inputText styleClass="inputText" required="false"
										requiredMessage="#{msg.donGia}: #{msg.valuerequired}"
										id="donGia" value="#{DonHangBean.dongia}">
									</p:inputText></td>
							</tr>


							<tr>
								<td></td>
								<td align="right"><p:commandButton value="Lưu"
										actionListener="#{DonHangBean.saveChiTietDonHang}"
										update=" :addChiTietDonHangForm:displayChiTietDonHang :addDonHangForm:display"
										oncomplete="if(args.opSuccess) PF('chiTietDonHangDialog').hide()">
									</p:commandButton></td>
							</tr>


						</table>
					</p:panel>

				</p:dialog>
			</h:form>

		</p:panel>

	</ui:define>
</ui:composition>
